<!-- 拼团商品列表 -->
<template>
	<view class="zg-area">
		<view class="fixed-tool-top">
			<!-- 分类1 -->
			<view class="fl1">
				<scroll-view scroll-x style="height: 40px">
					<view class="fltab">
						<view class="fltab-item fltab-item1" :class="{active: currentIndex1 === index}" v-for="(item,index) in nav1" :key="index"
						 @click="changeTab1(item,index)">
							{{item.name}}
						</view>
					</view>
				</scroll-view>
			</view>
			<!-- 分类2 -->
			<view class="fl2">
				<scroll-view scroll-x style="height: 40px">
					<view class="fltab">
						<view class="fltab-item" :class="{active: currentIndex2 === index}" v-for="(item,index) in nav2" :key="index"
						 @click="changeTab2(item,index)">
							{{item.name}}
						</view>
					</view>
				</scroll-view>
			</view>
			<!-- 筛选 -->
			<view class="sx">
				<scroll-view scroll-x style="height: 40px">
					<view class="sx-box">
						<view class="sx-box-item" :class="{active: currentIndex3 === 0}" @click="changeTab3(0)">
							<text>综合</text>
						</view>
						<view class="sx-box-item" :class="{active: currentIndex3 === 1}" @click="changeTab3(1)">
							<text>销量</text>
							<view class="sx-box-icon">
								<template v-if="currentIndex3 === 1">
									<template v-if="sortx === 1">
										<image src="/pagesShop/static/sf2.png" mode="widthFix"></image>
										<image src="/pagesShop/static/sf1-1.png" mode="widthFix"></image>
									</template>
									<template v-else>
										<image src="/pagesShop/static/sf2-1.png" mode="widthFix"></image>
										<image src="/pagesShop/static/sf1.png" mode="widthFix"></image>
									</template>
								</template>
								<template v-else>
									<image src="/pagesShop/static/sf2.png" mode="widthFix"></image>
									<image src="/pagesShop/static/sf1.png" mode="widthFix"></image>
								</template>
							</view>
						</view>
						<view class="sx-box-item" :class="{active: currentIndex3 === 2}" @click="changeTab3(2)">
							<text>价格</text>
							<view class="sx-box-icon">
								<template v-if="currentIndex3 === 2">
									<template v-if="sortx === 1">
										<image src="/pagesShop/static/sf2.png" mode="widthFix"></image>
										<image src="/pagesShop/static/sf1-1.png" mode="widthFix"></image>
									</template>
									<template v-else>
										<image src="/pagesShop/static/sf2-1.png" mode="widthFix"></image>
										<image src="/pagesShop/static/sf1.png" mode="widthFix"></image>
									</template>
								</template>
								<template v-else>
									<image src="/pagesShop/static/sf2.png" mode="widthFix"></image>
									<image src="/pagesShop/static/sf1.png" mode="widthFix"></image>
								</template>
							</view>
						</view>
						<view class="sx-box-item" :class="{active: currentIndex3 === 3}" @click="changeTab3(3)">
							<text>剩余时间</text>
							<view class="sx-box-icon">
								<template v-if="currentIndex3 === 3">
									<template v-if="sortx === 1">
										<image src="/pagesShop/static/sf2.png" mode="widthFix"></image>
										<image src="/pagesShop/static/sf1-1.png" mode="widthFix"></image>
									</template>
									<template v-else>
										<image src="/pagesShop/static/sf2-1.png" mode="widthFix"></image>
										<image src="/pagesShop/static/sf1.png" mode="widthFix"></image>
									</template>
								</template>
								<template v-else>
									<image src="/pagesShop/static/sf2.png" mode="widthFix"></image>
									<image src="/pagesShop/static/sf1.png" mode="widthFix"></image>
								</template>
							</view>
						</view>
						<view class="sx-box-item" @click="changeTab4(row_type)">
							<image v-if="row_type === 'row'" src="/pagesShop/static/f1.png" mode="aspectFill"></image>
							<image v-else src="/pagesShop/static/f2.png" mode="aspectFill"></image>
						</view>
					</view>
				</scroll-view>
			</view>
		</view>
		<view class="content">
			<view class="area-1" v-if="list.length > 0">
				<view v-if="row_type === 'row'">
					<view class="card" v-for="(item,index) in list" :key="index" @click="goRouter1(item)">
						<view class="card-left">
							<image class="tp-br12" :src="shopImgUrl + item.pic"  mode="aspectFill" @error="onImgLoadErr(list[index],'pic')"></image>
						</view>
						<view class="card-right">
							<view class="card-title ellispsis1">
								{{item.name}}
							</view>
							<view class="card-info">
								<view class="card-price">
									<text>￥{{item.ptPrice || 0}}</text>
									<text>￥{{item.price || 0}}</text>
								</view>
								<view class="card-info-right">
									<view class="card-xl">
										剩余{{item.stayNumber || 0}}件
									</view>
									<view class="card-xl">
										销量{{handleSales(item.sale)}}
									</view>
								</view>
							</view>
							<view class="card-tools">
								<view class="card-time">
									截至时间{{item.ptFinishTime || ''}}
								</view>
							</view>
						</view>
					</view>
				</view>
				<view class="card-wapper" v-else>
					<view class="card" v-for="(item,index) in list" :key="index" @click="goRouter1(item)">
						<view class="card-top">
							<image :src="shopImgUrl + item.pic" style="border-radius: 12rpx 12rpx 0 0;" mode="aspectFill" @error="onImgLoadErr(list[index],'pic')"></image>
						</view>
						<view class="card-bottom">
							<view class="card-title ellispsis1">
								{{item.name}}
							</view>
							<view class="card-info">
								<view class="card-price">
									<text>￥{{item.ptPrice || 0}}</text>
									<text>￥{{item.price || 0}}</text>
								</view>
							</view>
							<view class="card-info">
								<view class="card-xl">
									剩余{{item.stayNumber || 0}}件
								</view>
								<view class="card-xl">
									销量{{handleSales(item.sale)}}
								</view>
							</view>
							<view class="card-tools">
								<view class="card-time">
									截至时间{{item.ptFinishTime || ''}}
								</view>
							</view>
						</view>
					</view>
				</view>
				<uni-load-more color="#999" :status="status" v-if="list.length > 0"></uni-load-more>
			</view>
			<view class="nomore" v-if="list.length === 0 && !loading">
				<view class="nomore-text">
					<image src="/pagesShop/static/zanwu.png" mode="aspectFill"></image>
					<view>暂无数据</view>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				row_type: 'table',
				currentIndex1: 0,
				currentIndex2: 0,
				currentIndex3: 0,
				sortx: 1,
				categoryId: '',
				loading: false,
				page: 1,
				status: 'more',
				list: [],
				nav1: [],
				nav2: [],
			}
		},
		onLoad() {
			this.getList2()
		},
		onShow() {

		},
		onPullDownRefresh() {
			uni.stopPullDownRefresh()
			this.getList1()
		},
		onReachBottom() {
			this.getList1_1()
		},
		methods: {
			// 根据条件查询商品
			getList1() {
				this.page = 1
				this.loading = true
				this.$http.post(this.$api.shop_list_1, {}, {
					params: {
						firstCategoryId: this.firstCategoryId,
						categoryId: this.categoryId, // 商品二级分类id
						pageNum: this.page,
						status: this.currentIndex3, // 0综合 1销量 2价格
						sort: this.sortx, // 0升序 1降序
						type: 2
					}
				}).then(result => {
					this.loading = false
					if (result.code === 200) {
						const list = result.data.result;
						if (Array.isArray(list) && list.length > 0) {
							this.list = list
							if (this.list.length < 10) {
								this.status = 'noMore'
							}
						} else {
							this.list = []
							this.status = 'more'
						}
					} else {
						this.list = []
						this.status = 'more'
					}
				}).catch(() => {
					this.loading = false
					this.list = []
					this.status = 'more'
				})
			},
			// 根据条件查询商品
			getList1_1() {
				if (this.status === 'noMore') return
				this.page++
				this.status = 'loading'
				this.$http.post(this.$api.shop_list_1, {}, {
					params: {
						firstCategoryId: this.firstCategoryId,
						categoryId: this.categoryId, // 商品二级分类id
						pageNum: this.page,
						status: this.currentIndex3, // 0综合 1销量 2价格
						sort: 1, // 0升序 1降序
						type: 2
					}
				}).then(result => {
					if (result.code === 200) {
						const list = result.data.result;
						if (Array.isArray(list)) {
							if (list.length > 0) {
								this.list = this.list.concat(list);
							} else {
								this.status = 'noMore'
							}
						} else {
							this.status = 'more'
						}
					}
				}).catch(() => {
					this.status = 'more'
				})
			},
			// 获取商品全部类目
			getList2() {
				this.$http.post(this.$api.shop_tabs_1).then(result => {
					if (result.code === 200) {
						if (result.data.length) {
							result.data.unshift({
								name: '全部',
								id: ''
							})
						}
						this.nav1 = result.data;
						if (this.nav1.length > 0 && this.nav1[0]) {
							this.changeTab1(this.nav1[0], 0)
						}
					}
				})
			},
			changeTab1(item, index) {
				this.currentIndex1 = index
				this.firstCategoryId = item.id
				if (Array.isArray(this.nav1[index].mallProductCategories)) {
					this.nav2 = this.nav1[index] ? [{
						name: '全部'
					}].concat(this.nav1[index].mallProductCategories) : []
				} else {
					this.nav2 = [{
						name: '全部'
					}]
				}
				this.changeTab2('', 0)
			},
			changeTab2(item, index) {
				this.currentIndex2 = index
				this.categoryId = index > 0 ? item.id : ''
				this.getList1()
			},
			changeTab3(index) {
				this.currentIndex3 = index
				if(index === 1 || index === 2 || index === 3){ this.sortx = this.sortx === 1 ? 0 : 1}
				this.getList1()
			},
			changeTab4(row_type) {
				this.row_type = row_type === 'row' ? 'table' : 'row'
			},
			// 去商品详情
			goRouter1(item) {
				if (Object.prototype.toString.call(item) === '[object Object]' && typeof item.id === 'string' && item.id !== '') {
					uni.navigateTo({
						url: `../details/tg_details?cid=${item.id}`
					});
				} else {
					console.error('商品id错误');
				}
			}
		}
	}
</script>

<style lang="scss" scoped>
	.card-info-right {
		display: flex;

		&>view {
			margin-left: 10rpx;
		}
	}

	.card {
		margin: 30rpx;
		padding: 20rpx;
		background-color: #fff;
		border-radius: 10rpx;
		display: flex;
	}

	.card-left {
		flex-shrink: 0;
		width: 180rpx;
		height: 180rpx;
		overflow: hidden;
		margin-right: 20rpx;

		image {
			width: 180rpx;
			height: 180rpx;
		}
	}

	.card-right {
		flex-grow: 1;
	}

	.card-title {
		font-size: 28rpx;
		height: 40rpx;
	}

	.card-info {
		margin-top: 20rpx;
		display: flex;
		justify-content: space-between;
		align-items: flex-end;
	}

	.card-price {
		text {
			&:nth-of-type(1) {
				color: $uni-color-error;
				font-size: 36rpx;
				font-weight: bold;
			}

			&:nth-of-type(2) {
				margin-left: 10rpx;
				color: #999;
				text-decoration: line-through;
			}
		}
	}

	.card-xl {
		font-size: 24rpx;
		color: #999;
	}

	.card-tools {
		margin-top: 10rpx;
		display: flex;
		justify-content: space-between;
		align-items: center;
	}

	.card-time {
		margin-top: 20rpx;
		color: $uni-color-error;
		font-size: 22rpx;
	}

	.card-wapper {
		display: flex;
		flex-wrap: wrap;
		justify-content: space-between;
		margin: 30rpx;

		.card {
			margin: 0 0 30rpx;
			padding: 0;
			flex-direction: column;
			width: 330rpx;
		}

		.card-tools {
			flex-direction: column;
			align-items: flex-start;
			margin-bottom: 10rpx;
		}

		.card-info {
			margin-top: 10rpx;
		}

		.card-time {
			margin-top: 10rpx;
		}
	}

	.card-top {
		width: 330rpx;
		height: 330rpx;
	}

	.card-bottom {
		padding: 10rpx;
	}

	.fixed-tool-top {
		position: fixed;
		top: 0;
		left: 0;
		z-index: 1;
		width: 100%;
		height: 120px;
		box-sizing: border-box;
		background-color: #fff;
	}

	.fl1,
	.fl2 {
		border-bottom: 1px solid #eee;
		box-sizing: border-box;
	}

	.fltab {
		height: 100%;
		display: flex;
	}

	.fltab-item {
		display: flex;
		align-items: center;
		flex-shrink: 0;
		height: 100%;
		font-size: 26rpx;
		padding: 0 20rpx;
		position: relative;

		&.active {
			color: $blue;

			&::after {
				display: block;
			}
		}
	}

	.fltab-item1 {
		&::after {
			content: "";
			display: none;
			position: absolute;
			bottom: 0;
			left: 50%;
			margin-left: -0.5em;
			width: 1em;
			height: 6rpx;
			background-color: $blue;
		}

		&.active {
			&::after {
				display: block;
			}
		}
	}

	.sx-box {
		height: 100%;
		display: flex;
		justify-content: space-between;
		align-items: center;
	}

	.sx-box-item {
		height: 100%;
		display: flex;
		justify-content: center;
		align-items: center;
		flex-grow: 1;

		&>image {
			width: 36rpx;
			height: 36rpx;
		}

		&.active {
			color: $blue;
		}
	}

	.sx-box-icon {
		width: 30rpx;
		display: flex;
		flex-direction: column;
		justify-content: center;
		align-items: flex-end;

		image {
			width: 20rpx;
			height: 10rpx;

			&:nth-of-type(1) {
				margin-bottom: 4rpx;
			}
		}
	}

	.content {
		/* #ifdef MP-WEIXIN */
		padding: 120px 0 0;
		/* #endif */
		/* #ifdef H5 */
		padding: 70px 0 0;
		/* #endif */
	}
</style>
